package com.yima.baseproj.module.app.login;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yima.baseproj.config.redis.lock.Callback;
import com.yima.baseproj.config.redis.lock.LockConstact;
import com.yima.baseproj.config.redis.lock.RedisLockUtil;
import com.yima.baseproj.module.app.equipmentUser.EquipmentUserEntity;
import com.yima.baseproj.module.app.equipmentUser.EquipmentUserService;
import com.yima.baseproj.module.app.user.AppUserEntity;
import com.yima.baseproj.module.app.user.AppUserService;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.security.SecurityUtil;
import com.yima.baseproj.security.utils.jwt.JwtUtil;
import com.yima.baseproj.utils.MD5;
import com.yima.baseproj.utils.R;
import com.yima.baseproj.utils.wx.weapp.WxUtil;
import com.yima.baseproj.utils.wx.weapp.model.Code2SessionModel;
import com.yima.baseproj.utils.wx.weapp.model.UserPhoneModel;
import com.yima.baseproj.config.exception.type.ParamException;
import com.yima.baseproj.module.app.login.model.AppLoginMpWeixinParamModel;
import com.yima.baseproj.module.app.login.model.AppLoginMpWeixinPhoneParamModel;
import com.yima.baseproj.module.app.user.AppUserNoUtil;
import com.yima.baseproj.module.app.user.model.AppUserSexEnum;
import com.yima.baseproj.security.annotation.SecurityIgnoreUrl;
import com.yima.baseproj.security.exception.LoginAuthenticationException;
import com.yima.baseproj.security.model.UserLoginAuthModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "登录管理")
@RestController
@RequestMapping("/api/app/login")
public class AppLoginController extends BaseController {

    @Resource
    private AppUserService appUserService;

    @Autowired
    private RedisLockUtil redisLockUtil;

    @Value("${upload.webBasePath}")
    private String webbasepath;

    @Resource
    private EquipmentUserService equipmentUserService;

    @SecurityIgnoreUrl
    @ApiOperation("微信登录")
    @PostMapping(value = "/mpWeixin")
    public R<Map> mpWeixin(@RequestBody @Validated AppLoginMpWeixinParamModel paramModel) {
        Code2SessionModel jscode2session = WxUtil.getJscode2session(paramModel.getCode());

        if (StrUtil.isBlank(jscode2session.getOpenid())) {
            throw LoginAuthenticationException.LOGIN_JIEXI_ERROR;
        }

        //创建账号
        AppUserEntity appUserEntity = (AppUserEntity) redisLockUtil.execute(LockConstact.KEY_USER_CODE, 3, TimeUnit.SECONDS, new Callback() {
            @Override
            public Object onGetLock() {
                AppUserEntity userBean = appUserService.findByMpWeixinOpenId(jscode2session.getOpenid());
                EquipmentUserEntity userEmpBean = equipmentUserService.findByMpWeixinOpenId(jscode2session.getOpenid());
                if (userBean == null) {
                    userBean = new AppUserEntity();
                    userBean.setUserNo(AppUserNoUtil.createUserNo());
                    userBean.setMpWeixinOpenId(jscode2session.getOpenid());
                    userBean.setNickName("APP用户");
                    userBean.setPhone("");
                    userBean.setSex(AppUserSexEnum.WEIZHI.name());
                    userBean.setBirthday(DateUtil.date());
                    userBean.setHeadImg(String.format("%s/app/headimg/headimg.png", webbasepath));
                    appUserService.save(userBean);
                }
                if (userEmpBean == null) {
                    String encode = MD5.getMD5(jscode2session.getOpenid());
                    userEmpBean = new EquipmentUserEntity();
                    userEmpBean.setOpenid(jscode2session.getOpenid());
                    userEmpBean.setName("");
                    userEmpBean.setPhone("");
                    userEmpBean.setSex("");
                    userEmpBean.setPhone("");
                    userEmpBean.setHeadImg("");
                    userEmpBean.setMdFive(encode);
                    equipmentUserService.save(userEmpBean);
                }

                return userBean;
            }

            @Override
            public Object onTimeout() {
                //log.info("没拿到锁");
                return null;
            }
        });

        if (appUserEntity == null) {
            throw LoginAuthenticationException.USER_COUNT_MORE;
        }


//        设置用户信息
        UserLoginAuthModel authModel = new UserLoginAuthModel();
        authModel.setUserId(appUserEntity.getId());
        System.out.println("empSetUserId:" + appUserEntity.getId());

        authModel.setClient(SecurityUtil.getClient());
        System.out.println("empSecurityUtil.getClient():" + SecurityUtil.getClient());

        authModel.setRoleList(Arrays.asList());
        System.out.println("empSetRoleList:" + Arrays.asList());

        authModel.setPermissionList(Arrays.asList());
        System.out.println("empSetPermissionList:" + Arrays.asList());

        authModel.setUserEntity(appUserEntity);
        System.out.println("authModel:" + authModel);

        String token = JwtUtil.createToken(appUserEntity.getId(), SecurityUtil.getClient());
        System.out.println("token:" + token);

        SecurityUtil.setTokenToRedis(SecurityUtil.getClient(), appUserEntity.getId(), authModel);


        LambdaQueryWrapper<EquipmentUserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentUserEntity::getOpenid, jscode2session.getOpenid());
        EquipmentUserEntity one = equipmentUserService.getOne(queryWrapper);

        Map<String, String> map = new HashMap<String, String>();
        map.put("token", token);
        map.put("MD5", one.getMdFive());
        return R.success(map, "操作成功");
    }


}
